package com.system.service.impl;

import com.system.mapper.*;
import com.system.po.*;
import com.system.service.LeaderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LeaderServiceImpl implements LeaderService {


    @Autowired
    private LeaderMapper leaderMapper;

    @Autowired
    private LeaderMapperCustom leaderMapperCustom;

    @Autowired
    private CollegeMapper collegeMapper;



    @Override
    public void updateById(Integer id, LeaderCustom leaderCustom) throws Exception {
        leaderMapper.updateByPrimaryKey(leaderCustom);
    }

    @Override
    public void removeById(Integer id) throws Exception {

        leaderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<LeaderCustom> findByPaging(Integer toPageNo) throws Exception {
        PagingVO pagingVO = new PagingVO();
        pagingVO.setToPageNo(toPageNo);

        List<LeaderCustom> list = leaderMapperCustom.findByPaging(pagingVO);

        return list;
    }

    @Override
    public Boolean save(LeaderCustom leaderCustom) throws Exception {
        Leader tea = leaderMapper.selectByPrimaryKey(leaderCustom.getUserid());
        if (tea == null) {
            leaderMapper.insert(leaderCustom);
            return true;
        }
        return false;
    }

    @Override
    public int getCountLeader() throws Exception {
        //自定义查询对象
        LeaderExample leaderExample = new LeaderExample();
        //通过criteria构造查询条件
        LeaderExample.Criteria criteria = leaderExample.createCriteria();
        criteria.andUseridIsNotNull();

        return leaderMapper.countByExample(leaderExample);
    }

    @Override
    public LeaderCustom findById(Integer id) throws Exception {
        Leader leader = leaderMapper.selectByPrimaryKey(id);
        LeaderCustom leaderCustom = null;
        if (leader != null) {
            leaderCustom = new LeaderCustom();
            BeanUtils.copyProperties(leader, leaderCustom);
        }

        return leaderCustom;
    }

    @Override
    public List<LeaderCustom> findByName(String name) throws Exception {
        LeaderExample leaderExample = new LeaderExample();
        //自定义查询条件
        LeaderExample.Criteria criteria = leaderExample.createCriteria();

        criteria.andUsernameLike("%" + name + "%");

        List<Leader> list = leaderMapper.selectByExample(leaderExample);

        List<LeaderCustom> leaderCustomList = null;

        if (list != null) {
            leaderCustomList = new ArrayList<LeaderCustom>();
            for (Leader t : list) {
                LeaderCustom leaderCustom = new LeaderCustom();
                //类拷贝
                BeanUtils.copyProperties(t, leaderCustom);
                //获取课程名
                College college = collegeMapper.selectByPrimaryKey(t.getCollegeid());
                leaderCustom.setcollegeName(college.getCollegename());

                leaderCustomList.add(leaderCustom);
            }
        }

        return leaderCustomList;
    }

    @Override
    public List<LeaderCustom> findAll() throws Exception {
        LeaderExample leaderExample = new LeaderExample();
        LeaderExample.Criteria criteria = leaderExample.createCriteria();

        criteria.andUsernameIsNotNull();

        List<Leader> list = leaderMapper.selectByExample(leaderExample);
        List<LeaderCustom> leaderCustomsList = null;
        if (list != null) {
            leaderCustomsList = new ArrayList<LeaderCustom>();
            for (Leader l: list) {
                LeaderCustom leaderCustom = new LeaderCustom();
                BeanUtils.copyProperties(l, leaderCustom);
                leaderCustomsList.add(leaderCustom);
            }
        }
        return leaderCustomsList;
    }
}
